package com.wframe.project.jnd.util;

import org.apache.commons.lang.StringUtils;
import sun.net.util.IPAddressUtil;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * Created by Administrator on 2017/5/4.
 */
public class ServletUtils {

    public static boolean isNotAvailableIP(String sourceIP) {
        return (!isAvailableIP(sourceIP));
    }

    public static boolean isAvailableIP(String sourceIP) {
        String readyIP = StringUtils.trimToNull(sourceIP);
        if (readyIP == null) {
            return false;
        }
        if ("unknown".equalsIgnoreCase(readyIP)) {
            return false;
        }
        return (IPAddressUtil.isIPv4LiteralAddress(readyIP) || IPAddressUtil.isIPv6LiteralAddress(readyIP));
    }

    public static String getClientIP(HttpServletRequest request) {
        String ip = null;
        ip = request.getHeader("x-forwarded-for");
        if (isNotAvailableIP(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }

        if (isNotAvailableIP(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }

        if (isNotAvailableIP(ip)) {
            ip = request.getRemoteAddr();
        }

        if ("0:0:0:0:0:0:0:1".equals(ip)) {
            try {
                ip = InetAddress.getLocalHost().getHostAddress();
            } catch (UnknownHostException e) {
                e.printStackTrace();
            }
        }

        if (isNotAvailableIP(ip) && ip.indexOf(",") > 0) {
            ip = ip.substring(0, ip.indexOf(","));
        }

        if (isAvailableIP(ip)) {
            return ip;
        } else {
            return null;
        }
    }

    public static boolean isMobile(HttpServletRequest request) {
        boolean result = false;
        if (request != null) {
            String userAgent = request.getHeader("User-Agent");
            String mobileReg = "^.*(mobile|iphone|android|symbianos|windows\\sphone).*$";
            if (StringUtils.isNotEmpty(userAgent) && userAgent.toLowerCase().matches(mobileReg)) {
                result = true;
            }
        }
        return result;
    }

    public static boolean inWechat(HttpServletRequest request) {
        boolean result = false;
        if (request != null) {
            String userAgent = request.getHeader("User-Agent");
            if (StringUtils.isNotEmpty(userAgent) && userAgent.toLowerCase().lastIndexOf("micromessenger") >= 0) {
                result = true;
            }
        }
        return result;
    }

    public static String buildRequestUrlPrefix(HttpServletRequest request) {
        if (request != null) {
            StringBuffer url = new StringBuffer();
            url.append(request.getScheme());
            url.append("://");
            url.append(request.getServerName());
            if (request.getServerPort() > 0 && ("http".equalsIgnoreCase(request.getScheme()) && request.getServerPort() != 80 || "https".equalsIgnoreCase(request.getScheme()) && request.getServerPort() != 443)) {
                url.append(':');
                url.append(request.getServerPort());
            }
            return url.toString();
        }
        return null;
    }

}
